package com.wsc.wisdomchinaqinghe.httpUtils;

import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.lang.ref.WeakReference;

/**
 * 异步网络客户端
 *
 * @author jaily.zhang
 */
public class RequstClient {

    /**
     * post请求
     *
     * @param url     url 地址
     * @param params  参数
     * @param handler 数据加载句柄对象
     */
    public static void post(String url, RequestParams params, AsyncHttpResponseHandler handler) {
        try {
            if (params != null)
                Log.e("http地址：", "post:" + url + "?" + params.toString());
            else
                Log.e("http地址：", "post:" + url);
            AsyncHttpClient mClient = new AsyncHttpClient();
            WeakReference<AsyncHttpClient> softReferenceAsync = new WeakReference<AsyncHttpClient>(mClient);
            if (softReferenceAsync != null && softReferenceAsync.get() != null) {

                String accessKey = HttpHeadParams.getInstance().getAccessKey();
                if (accessKey != null) {
                    softReferenceAsync.get().addHeader("wscKey", accessKey);
                    Log.e("http","wscKey:"+accessKey);
                }
                String ts = HttpHeadParams.getInstance().getT();
                if (ts != null) {
                    softReferenceAsync.get().addHeader("wscT", ts);
                    Log.e("http","wscT:"+ts);
                }
                String token = HttpHeadParams.getInstance().getToken();
                if (token != null) {
                    softReferenceAsync.get().addHeader("wscToken", token);
                    Log.e("http","wscToken:"+token);
                }

                String uuid = HttpHeadParams.getInstance().getUuid();
                if (uuid != null)
                    softReferenceAsync.get().addHeader("uuid", uuid);

                String version = HttpHeadParams.getInstance().getVersion();
                if (version != null)
                    softReferenceAsync.get().addHeader("version", version);

                String os = HttpHeadParams.getInstance().getOs();
                if (os != null)
                    softReferenceAsync.get().addHeader("os", os);

//                String addr = HttpHeadParams.getInstance().getAddr();
//                if (addr != null) {
//                    String encode= URLEncoder.encode(addr,"UTF-8");
//                    softReferenceAsync.get().addHeader("addr", encode);
//                }

                String market = HttpHeadParams.getInstance().getMarket();
                if (market != null)
                    softReferenceAsync.get().addHeader("market", market);

                String mac = HttpHeadParams.getInstance().getMac();
                if (mac != null)
                    softReferenceAsync.get().addHeader("mac", mac);

                String imei = HttpHeadParams.getInstance().getImei();
                if (imei != null)
                    softReferenceAsync.get().addHeader("imei", imei);

                String mobile = HttpHeadParams.getInstance().getMobile();
                if (mobile != null)
                    softReferenceAsync.get().addHeader("mobile", mobile);

                String Ip = HttpHeadParams.getInstance().getIp();
                if (Ip != null)
                    softReferenceAsync.get().addHeader("Ip", Ip);
                String country = HttpHeadParams.getInstance().getCountry();
                if (country != null)
                    softReferenceAsync.get().addHeader("country", country);
                String region = HttpHeadParams.getInstance().getRegion();
                if (region != null)
                    softReferenceAsync.get().addHeader("region", region);
                String city = HttpHeadParams.getInstance().getCity();
                if (city != null)
                    softReferenceAsync.get().addHeader("city", city);
                String county = HttpHeadParams.getInstance().getCounty();
                if (county != null)
                    softReferenceAsync.get().addHeader("county", county);
                String isp = HttpHeadParams.getInstance().getIsp();
                if (isp != null)
                    softReferenceAsync.get().addHeader("isp", isp);

//                Log.e("http","wscKey:"+accessKey);
//                Log.e("http","wscT:"+ts);
//                Log.e("http","wscToken:"+token);
//
//                Log.e("http","uuid:"+uuid);
//                Log.e("http","version:"+version);
//                Log.e("http","os:"+os);
//
//                Log.e("http","addr:"+addr);
//                Log.e("http","mac:"+mac);
//                Log.e("http","imei:"+imei);
//
//                Log.e("http","mobile:"+mobile);
//                Log.e("http","market:"+market);

//                Log.e("http","Ip:"+Ip);
//                Log.e("http","country:"+country);
//                Log.e("http","region:"+region);
//                Log.e("http","city:"+city);
//                Log.e("http","county:"+county);
//                Log.e("http","isp:"+isp);

                // 重试三次,超时10S
                softReferenceAsync.get().setMaxRetriesAndTimeout(3, 10 * 1000);
                softReferenceAsync.get().post(url, params, handler);
            } else {

            }
        } catch (Exception e) {
            // TODO: handle exception
        }

    }

    /**
     * get 请求
     *
     * @param url     url地址
     * @param params  参数
     * @param handler handler句柄
     */
    public static void get(String url, RequestParams params, AsyncHttpResponseHandler handler) {
        try {
//            if (params != null)
//                Log.e("http地址：", "get:" + url + "?" + params.toString());
//            else
//                Log.e("http地址：", "get:" + url);
            AsyncHttpClient mClient = new AsyncHttpClient();
            WeakReference<AsyncHttpClient> softReferenceAsync = new WeakReference<AsyncHttpClient>(mClient);
            if (softReferenceAsync != null && softReferenceAsync.get() != null) {

                String accessKey = HttpHeadParams.getInstance().getAccessKey();
                if (accessKey != null) {
                    softReferenceAsync.get().addHeader("wscKey", accessKey);
                }
                String ts = HttpHeadParams.getInstance().getT();
                if (ts != null) {
                    softReferenceAsync.get().addHeader("wscT", ts);
                }
                String token = HttpHeadParams.getInstance().getToken();
                if (token != null) {
                    softReferenceAsync.get().addHeader("wscToken", token);
                }

                String uuid = HttpHeadParams.getInstance().getUuid();
                if (uuid != null)
                    softReferenceAsync.get().addHeader("uuid", uuid);

                String version = HttpHeadParams.getInstance().getVersion();
                if (version != null)
                    softReferenceAsync.get().addHeader("version", version);

                String os = HttpHeadParams.getInstance().getOs();
                if (os != null)
                    softReferenceAsync.get().addHeader("os", os);

//                String addr = HttpHeadParams.getInstance().getAddr();
//                if (addr != null) {
//                    String encode= URLEncoder.encode(addr,"UTF-8");
//                    softReferenceAsync.get().addHeader("addr", encode);
//                }

                String market = HttpHeadParams.getInstance().getMarket();
                if (market != null)
                    softReferenceAsync.get().addHeader("market", market);

                String mac = HttpHeadParams.getInstance().getMac();
                if (mac != null)
                    softReferenceAsync.get().addHeader("mac", mac);

                String imei = HttpHeadParams.getInstance().getImei();
                if (imei != null)
                    softReferenceAsync.get().addHeader("imei", imei);

                String mobile = HttpHeadParams.getInstance().getMobile();
                if (mobile != null)
                    softReferenceAsync.get().addHeader("mobile", mobile);

                String Ip = HttpHeadParams.getInstance().getIp();
                if (Ip != null)
                    softReferenceAsync.get().addHeader("Ip", Ip);
                String country = HttpHeadParams.getInstance().getCountry();
                if (country != null)
                    softReferenceAsync.get().addHeader("country", country);
                String region = HttpHeadParams.getInstance().getRegion();
                if (region != null)
                    softReferenceAsync.get().addHeader("region", region);
                String city = HttpHeadParams.getInstance().getCity();
                if (city != null)
                    softReferenceAsync.get().addHeader("city", city);
                String county = HttpHeadParams.getInstance().getCounty();
                if (county != null)
                    softReferenceAsync.get().addHeader("county", county);
                String isp = HttpHeadParams.getInstance().getIsp();
                if (isp != null)
                    softReferenceAsync.get().addHeader("isp", isp);

//                Log.e("http","wscKey:"+accessKey);
//                Log.e("http","wscT:"+ts);
//                Log.e("http","wscToken:"+token);
//
//                Log.e("http","uuid:"+uuid);
//                Log.e("http","version:"+version);
//                Log.e("http","os:"+os);
//
//                Log.e("http","addr:"+addr);
//                Log.e("http","mac:"+mac);
//                Log.e("http","imei:"+imei);
//
//                Log.e("http","mobile:"+mobile);
//                Log.e("http","market:"+market);
//                Log.e("http","Ip:"+Ip);
//                Log.e("http","country:"+country);
//                Log.e("http","region:"+region);
//                Log.e("http","city:"+city);
//                Log.e("http","county:"+county);
//                Log.e("http","isp:"+isp);

                // 重试三次,超时10S
                softReferenceAsync.get().setMaxRetriesAndTimeout(3, 10 * 1000);
                softReferenceAsync.get().get(url, params, handler);
            }
        } catch (Exception e) {
            // TODO: handle exception
        }

    }
}
